﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using Umzugsfirma.ViewModels;

namespace Umzugsfirma.Controllers
{
    public class OpportunityController : Controller
    {
        private DataModelContainer db = new DataModelContainer();

        //
        // GET: /Opportunity/

        public ActionResult Index()
        {
            System.Diagnostics.Stopwatch perfromanceWatch = new System.Diagnostics.Stopwatch();
            perfromanceWatch.Start();
            //Opportunity test = db.OpportunitySet.Create();
            Opportunity opportunity = new Opportunity
            {
                CreationDate = DateTime.Now
            };

            ContactDetails contDetails = new ContactDetails
                {
                    BusinessPhoneNumber = "0316602308",
                    City = "Bern",
                    CompanyName = "Trivadis",
                    EmailAddress = "caesar.gfeller@trivadis.com",
                    Forname = "Caesar",
                    HomePhoneNumber = "0317216415",
                    IsCompany = true,
                    MobilePhoneNumber = "0792967210",
                    Street1Address = "Worbstrasse 65",
                    Street2Address = "Postfach blabla",
                    Surname = "Caeasr",
                    ZipCode = 3113
                };

            Customer tempCust = new Customer
            {
                ContactDetails = contDetails
            };

            InvoiceAddress tempInv = new InvoiceAddress
            {
                ContactDetails = contDetails
            };

            Employee tempEmployee = new Employee
            {
                ContactDetails = contDetails
            };

            Offer tempOffer = new Offer
            {
                AcknowledgedDate = DateTime.Now,
                CreationDate = DateTime.Now,
                EmailSendDate = DateTime.Now,
                PrintDate = DateTime.Now,
                Employee = tempEmployee
            };

            Vehicle tempVehicle = new Vehicle
            {
                Make = "test ahahahaha"
            };

            opportunity.Customer = tempCust;
            opportunity.DischargePlace = null;
            opportunity.InvoiceAddress = tempInv;
            opportunity.Offer = tempOffer;
            opportunity.Employee = tempEmployee;

            perfromanceWatch.Stop();
            Console.WriteLine("Zeit um Objekte zu erstellen: {0}", perfromanceWatch.Elapsed);
            perfromanceWatch.Reset();

            perfromanceWatch.Start();

            db.OpportunitySet.Add(opportunity);
            db.SaveChanges();

            perfromanceWatch.Stop();
            Console.WriteLine("Zeit um Objekte in DB zu schreiben: {0}", perfromanceWatch.Elapsed);

            OpportunityIndexViewModel viewModel = new OpportunityIndexViewModel();
            foreach (Opportunity item in db.OpportunitySet)
            {
                viewModel.Opportunities.Add(item);
            }

            return View(viewModel);
        }

        //
        // GET: /Test/Details/5

        public ActionResult Details(string state, string id)
        {
            Opportunity opportunity;

            try
            {
                int idOfEntry = int.Parse(id);
                opportunity = db.OpportunitySet.Find(idOfEntry);
            }
            catch (System.Exception ex)
            {
//TODO: Ins log schreiben, da er keinen eintrag gefunden hat!
                opportunity = null;
            }

            if (opportunity == null)
            {
                return HttpNotFound();
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Test/Create

        public ActionResult Create(OpportunityCreateViewModel viewModel, string state)
        {
            viewModel = new OpportunityCreateViewModel
            {
                PageState = int.Parse(state),
                Items = db.TitlesSet.Select(t => new SelectListItem
                {
                    Value = t.TextDE,
                    Text = t.TextDE
                })
            };

            return View(viewModel);
        }

        //
        // POST: /Test/Create

        [HttpPost]
        public ActionResult Create(string submit, OpportunityCreateViewModel viewModel)
        {
            switch (submit)
            {
                case "Weiter":
                    viewModel.PageState++;
                    break;
                case "Zurück":
                    viewModel.PageState--;
                    break;
                case "Abrechen":
                    return RedirectToAction("Index", "Opportunity");
                default:
                    throw new Exception();
            }
#region     

            //if (ModelState.IsValid)
            //{
            //    //db.OpportunitySet.Add(viewModel.OneOpportunity);
            //    db.SaveChanges();
            //    return RedirectToAction("Index");
            //}
#endregion
            viewModel.Items = db.TitlesSet.Select(t => new SelectListItem
                {
                    Value = t.TextDE,
                    Text = t.TextDE
                });

            return View(viewModel);
        }

        //
        // GET: /Test/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Opportunity opportunity = db.OpportunitySet.Find(id);
            if (opportunity == null)
            {
                return HttpNotFound();
            }
            return View(opportunity);
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        public ActionResult Edit(Opportunity opportunity)
        {
            if (ModelState.IsValid)
            {
                db.Entry(opportunity).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(opportunity);
        }

        //
        // GET: /Test/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Opportunity opportunity = db.OpportunitySet.Find(id);
            if (opportunity == null)
            {
                return HttpNotFound();
            }
            return View(opportunity);
        }

        //
        // POST: /Test/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Opportunity opportunity = db.OpportunitySet.Find(id);
            db.OpportunitySet.Remove(opportunity);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
